<template>

    <!-- 对话框：审核 -->
    <el-dialog v-dialogDrag v-dialogDragWidth :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="相关信息：" prop="msg">
          {{form.msg}}
        </el-form-item>

        <el-input v-model="form.tableName" v-show="false" />
        <el-input v-model="form.fkUuid" v-show="false" />

        <el-form-item label="经办人姓名：">
          {{form.attentionUserNickName}}
        </el-form-item>

        <el-form-item label="审核人姓名：">
          {{form.confirmUserName}}
        </el-form-item>

        <el-form-item label="审核结果：" prop="oaDictConfirmStatus">
          <el-radio-group v-model="form.oaDictConfirmStatus">
            <el-radio
              v-for="dict in dict.type.oa_dict_confirm_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="审核意见：" prop="confirmNote" >
          <el-input v-model="form.confirmNote" type="textarea" placeholder="请输入内容" maxlength="50"
                    show-word-limit autosize/>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

</template>

<script>
import { listConfirm, getConfirm, delConfirm, addConfirm, updateConfirm, commonConfirm } from "@/api/oa/confirm";
import {getUserProfile} from "@/api/system/user";

export default {
  name: "Confirm",
  dicts: ['oa_dict_confirm_type', 'oa_dict_confirm_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      confirmParams: {},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审核表格数据
      confirmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeConfirmDt: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        confirmType: null,
        tableName: null,
        fkUuid: null,
        businessContent: null,
        attentionUserName: null,
        confirmUserName: null,
        confirmDt: null,
        confirmResult: null,
        confirmNote: null,
        logNote: null
      },
      // 表单参数
      form: {
        tableName: null,
      },
      // 表单校验
      rules: {
        confirmType: [
          { required: true, message: "审核类型不能为空", trigger: "change" }
        ],
        tableName: [
          { required: true, message: "表名不能为空", trigger: "blur" }
        ],
        fkUuid: [
          { required: true, message: "外键ID不能为空", trigger: "blur" }
        ],
        oaDictConfirmStatus: [
          { required: true, message: "请设定您的审核结果", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    /** 打开对话框：审核 */
    showDialog_confirm(confirmParams){
      console.log(confirmParams)
      confirmParams.oaDictConfirmStatus = 'pass'
      getUserProfile().then(response => {
        this.reset();
        this.open = true;
        this.title = "请您审核";
        this.form = confirmParams
        // this.form.fkUuid = confirmParams.fkUuid
        this.form.fkConfirmUserId = response.data.userId
        this.form.confirmUserName = response.data.nickName
      })

    },

    /** 审核完成 */
    confirm_done(){

      console.log( this.form )

      this.$refs["form"].validate(valid => {
        if (valid) {
          updateConfirm(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
          });
        }
      });
    },

    /** 查询审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeConfirmDt && '' != this.daterangeConfirmDt) {
        this.queryParams.params["beginConfirmDt"] = this.daterangeConfirmDt[0];
        this.queryParams.params["endConfirmDt"] = this.daterangeConfirmDt[1];
      }
      listConfirm(this.queryParams).then(response => {
        this.confirmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        confirmUuid: null,
        confirmType: null,
        tableName: null,
        fkUuid: null,
        businessContent: null,
        attentionUserName: null,
        confirmUserName: null,
        oaDictConfirmStatus: null,
        confirmDt: null,
        confirmResult: null,
        confirmNote: null,
        logNote: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeConfirmDt = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.confirmUuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审核";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const confirmUuid = row.confirmUuid || this.ids
      getConfirm(confirmUuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审核";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          this.getDicts("oa_dict_confirm_status").then(response => {
            console.log(response.data)
            response.data.forEach((item,index,arr) => {
              // if( item.dictValue == this.form.confirmResult ){
              //   this.form.oaDictConfirmStatus = item.dictLabel
              // }
              if( item.dictValue == this.form.oaDictConfirmStatus ){
                this.form.confirmResult = item.dictLabel
              }
              // arr[index] = item * document.getElementById("multiplyWith").value;
              // demo.innerHTML = numbers;
            })

            console.log( this.form )

            commonConfirm(this.form).then(response => {
              this.$modal.msgSuccess("审核成功");
              this.open = false;
              this.$emit('ok', [this.form]);
            });

          });

        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const confirmUuids = row.confirmUuid || this.ids;
      this.$modal.confirm('是否确认删除审核编号为"' + confirmUuids + '"的数据项？').then(function() {
        return delConfirm(confirmUuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/confirm/export', {
        ...this.queryParams
      }, `oa_confirm.xlsx`)
    }



  }
};
</script>
